#!/usr/bin/env bash

# 使用说明　注意修改ＩＰ地址　和　hostname，手动增加hosts
MASTERHOSTNAME="full-master-node"
MASTERIP="192.168.9.190"

# 关闭防火墙
systemctl disable firewalld.service 
systemctl stop firewalld.service

swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0
sysctl --system

# 设置系统参数 - 允许路由转发，不对bridge的数据进行处理 ???这个是不是有问题哦
cp ./k8s.conf /etc/sysctl.d/k8s.conf
# 生效配置文件
sysctl -p /etc/sysctl.d/k8s.conf

# 安装ｄｏｃｋｅｒ
yum install -y docker
systemctl enable docker
systemctl start docker

# 关闭 selinu
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux

# etcd安装
yum install -y etcd
systemctl daemon-reload
systemctl start etcd
systemctl status etcd.service

mkdir /var/log/kubernetes
mkdir /etc/kubernetes
mkdir /etc/kubernetes/ssl



# kube-apiserver
cp ./kube-apiserver.service /usr/lib/systemd/system/kube-apiserver.service
cp ./apiserver /etc/kubernetes/apiserver 

# kube-controller-manager
cp ./kube-controller-manager.service  /usr/lib/systemd/system/kube-controller-manager.service 
cp ./controller-manager /etc/kubernetes/controller-manager 

#　kube-scheduler
cp ./kube-scheduler.service /usr/lib/systemd/system/kube-scheduler.service 
cp ./scheduler /etc/kubernetes/scheduler 

# /etc/kubernetes/kubeconfig
cp ./kubeconfig /etc/kubernetes/kubeconfig

# 证书
cp ./master_ssl.cnf /etc/kubernetes/ssl/
cd  /etc/kubernetes/ssl/
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=${MASTERIP}" -days 5000 -out ca.crt    # CN指定Master的IP地址
openssl genrsa -out server.key 2048

openssl req -new -key server.key -subj "/CN=${MASTERHOSTNAME}" -config master_ssl.cnf -out server.csr   # CN指定主机名
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 5000 -extensions v3_req -extfile master_ssl.cnf -out server.crt

# 设置kube-controller-manager相关证书
openssl genrsa -out cs_client.key 2048
openssl req -new -key cs_client.key -subj "/CN=${MASTERHOSTNAME}" -out cs_client.csr     # CN指定主机名
openssl x509 -req -in cs_client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out cs_client.crt -days 5000

systemctl daemon-reload
systemctl enable kube-apiserver
systemctl enable kube-controller-manager
systemctl enable kube-scheduler

systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler

systemctl status kube-apiserver
systemctl status kube-controller-manager
systemctl status kube-scheduler

